// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mybaseplus/base/abstract/IFFScaffold.dart';
import 'package:mybaseplus/base/tool/color/FFColor.dart';
import 'package:mybaseplus/base/tool/globalMethod/FFSafeValue.dart';
import 'package:mybaseplus/base/widget/layout/FFImageButton.dart';
import 'package:mybaseplus/base/widget/page/FFScaffold.dart';
import 'package:mybaseplus_example/const/ImgConst.dart';
import 'package:mybaseplus_example/pages/home/TestMain.dart';

class TabbarPage extends FFScaffold {
  String? name;

  TabbarPage({Key? key, this.name}) : super(key: key);

  @override
  TabbarPageState createState() => TabbarPageState();
}

class TabbarPageState extends FFScaffoldState with MFFScaffold {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  TabbarPage get widget => super.widget as TabbarPage;

  /**固定写法end */
  @override
  FFPageStatus? get ffPageStatus => FFPageStatus.result;

  var currentIndex = 0;
  late List<TabbarItemEntity> items;
  List<Widget> tabbarChild = [
    // MultiLanguagePage(),
    TestMain(),
    Container(),
    Container(),
  ];

  @override
  void ffInitState() {
    super.ffInitState();
  }

  @override
  Widget? ffBottomNavigationBar(BuildContext context) {
    return Theme(
      data: ThemeData(
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        primarySwatch: FFColor.createMaterialColor(Colors.black),
        primaryColor: FFColor.createMaterialColor(Colors.black),
      ),
      child: BottomNavigationBar(
        // elevation: 0,
        backgroundColor: Colors.transparent,
        selectedItemColor: Colors.red,
        unselectedItemColor: Colors.white,
        items: item(context, currentIndex),
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          _changePage(index);
        },
      ),
    );
  }

  @override
  Widget? ffBody(BuildContext context) {
    return IndexedStack(index: currentIndex, children: tabbarChild);
  }

  List<BottomNavigationBarItem> item(BuildContext context, int index) {
    items = [
      TabbarItemEntity(
        icon: ImgConst.imgred1,
        norIcon: ImgConst.imgred2,
        title: "1",
      ),
      TabbarItemEntity(
        icon: ImgConst.imgred1,
        norIcon: ImgConst.imgred2,
        title: "2",
      ),
      TabbarItemEntity(
        icon: ImgConst.imgred1,
        norIcon: ImgConst.imgred2,
        title: "3",
      ),
    ];

    List<BottomNavigationBarItem> list = [];

    for (int i = 0; i < items.length; i++) {
      TabbarItemEntity itemEntity = items[i];
      list.add(BottomNavigationBarItem(
        icon: FFImageButton(
          width: 24.w,
          heigth: 24.w,
          child: FFImageFactory.asset(i == index ? itemEntity.icon : itemEntity.norIcon,
              color: i == index ? Colors.blue : Colors.grey),
        ),
        label: itemEntity.title,
      ));
    }

    return list;
  }

  /*切换页面*/
  Future<void> _changePage(int index) async {
    if (index != currentIndex) {
      handle(index);
      currentIndex = index;
      setState(() {});
    }
  }

  handle(int index) {
    var wid = globalSafeValueFromList(tabbarChild, index);
    assert(wid != null,"这儿不应该为空的");
    if (wid is Container) {
      switch (index) {
        case 0:
          break;
        case 1:
          tabbarChild[index] = Container();
          break;
        case 2:
          tabbarChild[index] = Container();
          break;
        case 3:
          tabbarChild[index] = Container();
          break;
        default:
      }
    }
  }
}

class TabbarItemEntity {
  String icon;
  String norIcon;
  String title;

  TabbarItemEntity({
    required this.icon,
    required this.title,
    required this.norIcon,
  });
}
